package com.yf.thumbbackend.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.thumbbackend.constant.RedisLuaScriptConstant;
import com.yf.thumbbackend.entity.Thumb;
import com.yf.thumbbackend.entity.User;
import com.yf.thumbbackend.entity.dto.DoThumbRequest;
import com.yf.thumbbackend.entity.enums.LuaStatusEnum;
import com.yf.thumbbackend.exception.BusinessException;
import com.yf.thumbbackend.mapper.ThumbMapper;
import com.yf.thumbbackend.service.BlogService;
import com.yf.thumbbackend.service.ThumbService;
import com.yf.thumbbackend.service.UserService;
import com.yf.thumbbackend.util.RedisKeyUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
* @author echo-sp
* @description 针对表【thumb】的数据库操作Service实现
* @createDate 2025-04-18 20:27:12
*/
@Service("thumbServiceRedis")
@RequiredArgsConstructor
public class ThumbServiceRedisImpl extends ServiceImpl<ThumbMapper, Thumb>
    implements ThumbService{

    @Resource
    private UserService userService;
    @Resource
    private BlogService blogService;

    private final RedisTemplate<String,Object> redisTemplate;



    @Override
    public Boolean doThumb(DoThumbRequest doThumbRequest, HttpServletRequest request) {
        //校验
        if(doThumbRequest == null || doThumbRequest.getId() == null){
            throw  new BusinessException(400,"参数异常");
        }
        //获取登录用户
        User loginUser = userService.getLoginUser(request);
        Long blogId = doThumbRequest.getId();

        String timeSlice = getTimeSlice();
        String tempThumbKey = RedisKeyUtil.getTempThumbKey(timeSlice);
        String userThumbKey = RedisKeyUtil.getUserThumbKey(loginUser.getId());
        //执行Lua点赞脚本
        Long execute = redisTemplate.execute(
                RedisLuaScriptConstant.THUMB_SCRIPT,
                Arrays.asList(tempThumbKey, userThumbKey),
                loginUser.getId(),
                blogId
        );
        if(execute == LuaStatusEnum.FAIL.getValue()){
            throw new BusinessException(400,"点赞失败");
        }
        return LuaStatusEnum.SUCCESS.getValue() == execute;


    };;

    @Override
    public Boolean undoThumb(DoThumbRequest doThumbRequest, HttpServletRequest request) {
        if(doThumbRequest == null || doThumbRequest.getId() == null){
            throw  new BusinessException(400,"参数异常");
        }
        User loginUser = userService.getLoginUser(request);
        Long blogId = doThumbRequest.getId();
        String timeSlice = getTimeSlice();
        String tempThumbKey = RedisKeyUtil.getTempThumbKey(timeSlice);
        String userThumbKey = RedisKeyUtil.getUserThumbKey(loginUser.getId());
        //执行Lua取消点赞脚本
        Long execute = redisTemplate.execute(
RedisLuaScriptConstant.UNTHUMB_SCRIPT,
                Arrays.asList(tempThumbKey, userThumbKey),
         loginUser.getId(),
         blogId
        );
        if(execute == LuaStatusEnum.FAIL.getValue()){
            throw new BusinessException(400,"用户未点赞");
        }
        return LuaStatusEnum.SUCCESS.getValue() == execute;

    }



    @Override
    public Boolean hasThumb(Long blogId, Long userId) {
        return redisTemplate.opsForHash().hasKey(RedisKeyUtil.getUserThumbKey(userId), blogId);
    }
    private String getTimeSlice(){
        DateTime nowDate = DateUtil.date();

        return DateUtil.format(nowDate, "HH:mm") + (DateUtil.second(nowDate) / 10) *10;
    }
}




